package com.yanqu.road.dao.impl.wingroom;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.wingroom.message.WingRoomMessageDetailInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2022/3/19 17:03
 */
public class WingRoomMessageBoardDaoImpl extends TempDataObjectDao<WingRoomMessageDetailInfo> {
    public static String insertSql = "INSERT INTO t_u_wingroom_message_board(userId,messageSwitch,selfMessageContent," +
            "selfMessageGiveLikeTimes,lastAccessTime,ownerLastRequestTime,maxMessageId) values(?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(WingRoomMessageDetailInfo data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMessageSwitch()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSelfMessageContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSelfMessageGiveLikeTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastAccessTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOwnerLastRequestTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxMessageId()));

        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE t_u_wingroom_message_board set messageSwitch=?,selfMessageContent=?,selfMessageGiveLikeTimes=?," +
            "lastAccessTime=?,ownerLastRequestTime=?,maxMessageId=? where userId=?";
    @Override
    protected boolean updateData(WingRoomMessageDetailInfo data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getMessageSwitch()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getSelfMessageContent()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSelfMessageGiveLikeTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastAccessTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getOwnerLastRequestTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getMaxMessageId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected WingRoomMessageDetailInfo parseData(ResultSet rs) throws SQLException {
        WingRoomMessageDetailInfo data = new WingRoomMessageDetailInfo();
        data.setUserId(rs.getLong("userId"));
        data.setSelfMessageContent(rs.getString("selfMessageContent"));
        data.setSelfMessageGiveLikeTimes(rs.getLong("selfMessageGiveLikeTimes"));
        data.setLastAccessTime(rs.getLong("lastAccessTime"));
        data.setOwnerLastRequestTime(rs.getLong("ownerLastRequestTime"));
        data.setMaxMessageId(rs.getLong("maxMessageId"));
        data.setMessageSwitch(rs.getInt("messageSwitch"));
        return data;
    }

    public Map<Long, WingRoomMessageDetailInfo> getWingRoomMessageDetailInfoMap(long accessTimeBegin) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, WingRoomMessageDetailInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_wingroom_message_board where lastAccessTime >= " + accessTimeBegin;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                WingRoomMessageDetailInfo data = parse(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public WingRoomMessageDetailInfo getWingRoomMessageDetailInfo(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_wingroom_message_board where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }
}
